package jgl.vajra.management.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.fragment.app.FragmentActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import jgl.vajra.management.R;
import jgl.vajra.management.widget.base.BaseCustomView;
import jgl.vajra.management.databinding.ViewToolsBarLayoutBinding;
import jgl.vajra.management.widget.utils.UIUtils;
import jgl.vajra.management.widget.interfaces.ToolsBarLayoutClickListener;

public class ToolsBarLayout extends BaseCustomView<ViewToolsBarLayoutBinding> {
    private ToolsBarLayoutClickListener mToolsBarLayoutClickListener;

    public ToolsBarLayout(Context context) {
        super(context);
    }

    public ToolsBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public int rightTextColor = UIUtils.getColor(R.color.black);
    public int titleColor = UIUtils.getColor(R.color.white);
    public boolean isShowRightImage = false;
    public boolean isShowRightText = false;
    public boolean isLeftImageExit = true;
    public int leftImage = R.drawable.ic_left_arrow;
    public int rightImage = R.drawable.ic_left_arrow;
    public String rightText;
    public String titleText;

    @Override
    protected int getLayoutId() {
        return R.layout.view_tools_bar_layout;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        AppCompatActivity activity = (AppCompatActivity) context;
        activity.setSupportActionBar(mBinding.toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToolsBarLayout);
        rightTextColor = a.getColor(R.styleable.ToolsBarLayout_rightTextColor, rightTextColor);
        titleColor = a.getColor(R.styleable.ToolsBarLayout_titleColor, titleColor);
        rightText = a.getString(R.styleable.ToolsBarLayout_rightText);
        titleText = a.getString(R.styleable.ToolsBarLayout_titleText);
        isShowRightImage = a.getBoolean(R.styleable.ToolsBarLayout_showRightImage, isShowRightImage);
        isShowRightText = a.getBoolean(R.styleable.ToolsBarLayout_showRightText, isShowRightText);
        isLeftImageExit = a.getBoolean(R.styleable.ToolsBarLayout_isLeftImageExit, isLeftImageExit);
        leftImage = a.getResourceId(R.styleable.ToolsBarLayout_leftImage, leftImage);
        int rightImageRes = a.getResourceId(R.styleable.ToolsBarLayout_rightImage, rightImage);
        if (rightImage != rightImageRes) {
            mBinding.ivRightRight.setImageResource(rightImageRes);
        }
        mBinding.ivRightRight.setVisibility(isShowRightImage ? VISIBLE : GONE);
        mBinding.tvRightText.setVisibility(isShowRightText ? VISIBLE : GONE);
        mBinding.ivLeftImage.setImageResource(leftImage);
        mBinding.tvRightText.setTextColor(rightTextColor);
        //  mBinding.tvToolbar.setTextColor(titleColor);
        mBinding.tvRightText.setText(rightText);
        // mBinding.tvToolbar.setText(titleText);
        mBinding.setView(this);

        int titleGravity = a.getInteger(R.styleable.ToolsBarLayout_titleGravity, 0);
        if (titleGravity == 1) {
            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.CENTER_IN_PARENT);
            mBinding.tvToolbar.setLayoutParams(p);
        }
        a.recycle();
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    public void leftImageClick() {
        if (isLeftImageExit) ((FragmentActivity) getContext()).finish();
        if (mToolsBarLayoutClickListener != null)
            mToolsBarLayoutClickListener.leftImageClick(this, getContext());

    }

    public void rightImageClick() {
        if (mToolsBarLayoutClickListener != null)
            mToolsBarLayoutClickListener.rightImageClick(this, getContext());

    }

    public void rightTextClick() {
        if (mToolsBarLayoutClickListener != null)
            mToolsBarLayoutClickListener.rightTextClick(this, getContext());

    }

    public void setToolsBarLayoutClickListener(ToolsBarLayoutClickListener layoutClickListener) {
        this.mToolsBarLayoutClickListener = layoutClickListener;
    }
}
